package com.tseacher.service.impl;

import com.tseacher.dao.LinkDao;
import com.tseacher.pojo.Link;
import com.tseacher.service.LinkService;
import com.tseacher.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;

    @Override
    public void add(Link link) {
        linkDao.add(link);
    }

    @Override
    public Page<Link> getPage(Integer cpage, Integer rows) {
        //1、查询总数
        int count = linkDao.selectCount();
        //2、计算总页数
        rows = rows<=0?5:rows;
        int tpage = count%rows==0?count/rows:count/rows+1;
        //3、判断cpage
        cpage = cpage<=0?1:cpage;
        cpage = cpage>tpage?tpage:cpage;
        //4、查询分页集合
        List<Link> linkList = linkDao.selectPage(cpage,rows);
        //5、封装
        Page<Link> page = new Page<>();
        page.setCpage(cpage);
        page.setTpage(tpage);
        page.setRows(rows);
        page.setList(linkList);
        return page;
    }

    @Override
    public void deleteLink(Integer id) {
        linkDao.deleteLink(id);
    }
}
